package random.fox.p2p.ui.customview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import random.fox.p2p.R;
import random.fox.p2p.bean.Fans;
import random.fox.p2p.ui.customview.imageLoader.CircleBitmapDisplayer;

/**
 * Created by delong on 2015/3/19.
 */
public class FansAdapter extends BaseAdapter {

    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private Context context;
    private List<Fans> list;
    private int itemWidth;

    private boolean isNeedExpand;
    private int temp;

    public FansAdapter(List<Fans> list, int itemWidth, int temp, boolean isNeedExpand) {
        this.list = list;
        this.itemWidth = itemWidth;
        this.isNeedExpand = isNeedExpand;
        this.temp = temp;
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
//                .showStubImage(R.drawable.ic_stub)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new CircleBitmapDisplayer())  // 设置成圆角图片
                .build();
    }

    @Override
    public int getCount() {
        int count = 0;
        if (isNeedExpand) {
            count = temp;
        } else {
            count = list.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if(context == null)
            context = parent.getContext();
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.fans_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        Fans fans = list.get(position);
        convertView.setLayoutParams(new AbsListView.LayoutParams(itemWidth, itemWidth));

        String url = fans.getHeadUrl();
        String fansName = fans.getName();

        holder.fansNameText.setText(fansName);
        int width = itemWidth * 3 / 4;

        holder.fansImageView.setLayoutParams(new LinearLayout.LayoutParams(width, width));

        if(isNeedExpand && position == temp-1){
            holder.fansNameText.setVisibility(View.INVISIBLE);
            imageLoader.displayImage("drawable://" + R.drawable.expand, holder.fansImageView, options);
        }else{
            holder.fansNameText.setVisibility(View.VISIBLE);
            imageLoader.displayImage(url, holder.fansImageView, options);
        }

        return convertView;
    }

    class ViewHolder {
        @InjectView(R.id.fans_head_img)
        ImageView fansImageView;
        @InjectView(R.id.fans_name)
        TextView fansNameText;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }

    }
}
